<?php
/**
 * 修复权限配置菜单的 node 字段
 * 确保它为空字符串，这样就不会被权限过滤
 */

$host = '113.44.136.17';
$port = 3306;
$database = 'qiandao';
$username = 'qiandao';
$password = 'NGbks3HWx5PhSxDX';

try {
    $pdo = new PDO("mysql:host={$host};port={$port};dbname={$database};charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "=== 修复权限配置菜单 ===\n\n";
    
    // 检查当前状态
    $stmt = $pdo->query("SELECT id, title, url, node FROM system_menu WHERE id = 43");
    $menu = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if ($menu) {
        echo "当前状态：\n";
        echo "  ID: {$menu['id']}\n";
        echo "  标题: {$menu['title']}\n";
        echo "  URL: {$menu['url']}\n";
        echo "  NODE: " . ($menu['node'] === '' ? '(空字符串)' : "'{$menu['node']}'") . "\n";
        echo "  NODE 是否为 NULL: " . (is_null($menu['node']) ? '是' : '否') . "\n\n";
        
        // 确保 node 为空字符串
        $stmt = $pdo->prepare("UPDATE system_menu SET node = '', params = '' WHERE id = 43");
        $stmt->execute();
        
        echo "✓ 已将 node 和 params 设置为空字符串\n\n";
        
        // 验证
        $stmt = $pdo->query("SELECT id, title, url, node, params FROM system_menu WHERE id = 43");
        $menu = $stmt->fetch(PDO::FETCH_ASSOC);
        
        echo "修复后状态：\n";
        echo "  NODE: " . ($menu['node'] === '' ? '(空字符串)' : "'{$menu['node']}'") . "\n";
        echo "  PARAMS: " . ($menu['params'] === '' ? '(空字符串)' : "'{$menu['params']}'") . "\n";
        
    } else {
        echo "❌ 找不到 ID=43 的菜单\n";
    }
    
    // 同时检查所有分组菜单（URL=#的菜单）
    echo "\n=== 检查所有分组菜单 ===\n";
    
    $stmt = $pdo->query("
        SELECT id, pid, title, url, node 
        FROM system_menu 
        WHERE url = '#' AND status = 1 AND pid > 0
        ORDER BY pid, sort DESC, id ASC
    ");
    
    $groupMenus = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "找到 " . count($groupMenus) . " 个分组菜单：\n\n";
    
    foreach ($groupMenus as $menu) {
        $nodeStatus = ($menu['node'] === '' || is_null($menu['node'])) ? '✓ 空' : "❌ '{$menu['node']}'";
        echo "  {$menu['title']} (ID:{$menu['id']}, PID:{$menu['pid']}) - NODE: {$nodeStatus}\n";
        
        // 如果 node 不为空，修复它
        if ($menu['node'] !== '' && !is_null($menu['node'])) {
            $stmt = $pdo->prepare("UPDATE system_menu SET node = '', params = '' WHERE id = ?");
            $stmt->execute([$menu['id']]);
            echo "    → 已修复\n";
        }
    }
    
    echo "\n✓ 完成！\n";
    echo "\n请清除缓存并刷新后台页面\n";
    
} catch (PDOException $e) {
    echo "数据库错误: " . $e->getMessage() . "\n";
}

